/**
 * @ClassName: SysPriceUpdateRecordUtils
 * @Description:  价格修改记录表
 * @author wuzujian
 * @date 2019-10-22
 */

package com.fzrskj.cloud.free.sys.utils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fzrskj.cloud.free.sys.bean.SysPriceUpdateRecordBean;
import com.fzrskj.cloud.free.sys.dao.SysPriceUpdateRecordDao;
import ms.core.object.CObject;
import ms.core.consts.SysErr;

public class SysPriceUpdateRecordUtils extends CObject {
    //private declarations
    private SysPriceUpdateRecordDao dao = null;

    //constructor function to initialize database access object
    public SysPriceUpdateRecordUtils() {
        dao = new SysPriceUpdateRecordDao();
    }

    //to free resource
    @Override
    public void dispose() {
        if (dao != null) dao.dispose();
        dao = null;
        super.dispose();
    }

    //new record by map
    public int newSysPriceUpdateRecord(Map<String, Object> values) {
        int ret = dao.newSysPriceUpdateRecord(values);
        this.setErrCode(dao.getErrCode());
        return ret;
    }

    //delete record by id
    public int delSysPriceUpdateRecord(int id) {
        int ret = dao.delSysPriceUpdateRecord(id);
        this.setErrCode(dao.getErrCode());
        if (ret == 0) this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
        return ret;
    }

    //modify record by map
    public int modSysPriceUpdateRecord(Map<String, Object> values, Map<String, Object> cdt) {
        int ret = dao.modSysPriceUpdateRecord(values, cdt);
        this.setErrCode(dao.getErrCode());
        if (ret == 0) this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
        return ret;
    }

    //query record by id
    public SysPriceUpdateRecordBean querySysPriceUpdateRecord(int id) {
        SysPriceUpdateRecordBean ret = dao.querySysPriceUpdateRecord(id);
        String err = dao.getErrCode();
        this.setErrCode(err);
        if (err.equals(SysErr.CODE_SUCCESS) && ret == null) this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
        return ret;
    }

    //query record count by cdt
    public int querySysPriceUpdateRecord(Map<String, Object> cdt) {
        int ret = dao.querySysPriceUpdateRecord(cdt);
        this.setErrCode(dao.getErrCode());
        return ret;
    }

    //query record list by cdt
    public List<SysPriceUpdateRecordBean> querySysPriceUpdateRecord(Map<String, Object> cdt, String orderby, int page, int pagesize) {
        List<SysPriceUpdateRecordBean> rets = dao.querySysPriceUpdateRecord(cdt, orderby, page, pagesize);
        String err = dao.getErrCode();
        this.setErrCode(err);
        if (err.equals(SysErr.CODE_SUCCESS) && (rets == null || rets.size() == 0)) {
            this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
        }
        return rets;
    }

    /**
     * 添加修改价格记录
     * @param userName
     * @param userId
     * @param content
     * @param remark
     * @return
     */
    public static int addServicePriceUpdateRecord(String userName, int userId, String content, String remark) {
        return addUpdateRecord(userName, userId, 1, content, remark);
    }

    /**
     * 添加修改价格记录
     * @param userName
     * @param userId
     * @param type
     * @param content
     * @param remark
     * @return
     */
    public static int addUpdateRecord(String userName, int userId, int type, String content, String remark) {
        Map<String, Object> values = new HashMap<>();
        values.put("userName", userName);
        values.put("userId", userId);
        values.put("type", type);
        values.put("content", content);
        values.put("remark", remark);
        values.put("addTime", new Date());
        SysPriceUpdateRecordDao dao = new SysPriceUpdateRecordDao();
        int ret = dao.newSysPriceUpdateRecord(values);
        dao.dispose();
        return ret;
    }
}